#include "StdAfx.h"
#include "SimulatorControl.h"

int SimulatorControl::nameLenMax = 16;
int SimulatorControl::portNumMax = 3;

SimulatorControl::SimulatorControl(void)
:
	setSensorModeFp(0),
	setLightBrightnessFp(0),
	setLightStatusFp(0),
	sendMessageFp(0),
	receiveMessageFp(0),
	clearMessageFp(0),
	orderMotorFp(0),
	cputCLCDFp(0),
	cputWLCDFp(0),
	cputSLCDFp(0),
	clearLCDFp(0),
	playFp(0),
	setSoundDurationFp(0),
	setSoundInternoteFp(0),
	stopSoundFp(0),
	beepSoundFp(0),
	notifyLoopCountFp(0),
	keyTaskDelegator(0), msgTaskDelegator(0)
{
	for (int i=0;i<portNumMax;i++) {
		this->sensorName[i] = new char[nameLenMax];
		this->sensorName[i][0] = '\0';
		this->motorName[i] = new char[nameLenMax];
		this->motorName[i][0] = '\0';
	}
	this->sensorValue[0]=0;
	this->sensorValue[1]=0;
	this->sensorValue[2]=0;

}

SimulatorControl::~SimulatorControl(void)
{
	for (int i=0;i<portNumMax;i++) {
		delete [] this->sensorName[i];
		delete [] this->motorName[i];
	}

}

int SimulatorControl::getSensorValue(int port)
{
	return this->sensorValue[port];
}

void SimulatorControl::setSensorMode(int port, bool active)
{
	if (this->setSensorModeFp==0) return;
	this->setSensorModeFp(port, active);
}

void SimulatorControl::setLightBrightness(int port, int level)
{
	if (this->setLightBrightnessFp==0) return;
	this->setLightBrightnessFp(port, level);
}

void SimulatorControl::setLightStatus(int port, bool on)
{
	if (this->setLightStatusFp==0) return;
	this->setLightStatusFp(port,on);
}

int SimulatorControl::getBatteryValue()
{
	return this->batteryLevel;
}

int SimulatorControl::getKeyStatus(int keys, bool isPressed)
{
	return this->keyStatus;
}

unsigned long SimulatorControl::registKeyDelegator(ISimulatorControl::TaskDelegator fp)
{
	this->keyTaskDelegator = fp;
	return 0;
}

void SimulatorControl::sendMessage(unsigned char msg)
{
	if (this->sendMessageFp==0) return;
	this->sendMessageFp(msg);
}

unsigned char SimulatorControl::receiveMessage(bool untilBlock)
{
	if (this->receiveMessageFp==0) return 0;
	return this->receiveMessageFp(untilBlock);
}
void SimulatorControl::clearMessage()
{
	if (this->clearMessageFp==0) return;
	this->clearMessageFp();
}

int SimulatorControl::registMessageDelegator(ISimulatorControl::TaskDelegator fp)
{
	this->msgTaskDelegator=fp;
	return 0;
}

void SimulatorControl::orderMotor(int port, char* methodName, int param)
{
	if (this->orderMotorFp==0) return;
	orderMotorFp(port, methodName, param);
}

void SimulatorControl::cputcLCD(ISimulatorControl::LCD_DISPLAY_MODE mode, char c, int pos)
{
	if (this->cputCLCDFp==0) return;
	this->cputCLCDFp(mode,c,pos);
}
void SimulatorControl::cputwLCD(unsigned word)
{
	if (this->cputWLCDFp==0) return;
	this->cputWLCDFp(word);
}
void SimulatorControl::cputsLCD(char* s)
{
	if (this->cputSLCDFp==0) return;
	this->cputSLCDFp(s);
}
void SimulatorControl::clearLCD()
{
	if (this->clearLCDFp==0) return;
	this->clearLCDFp();
}

void SimulatorControl::play(unsigned char pitch, unsigned char length)
{
	if (this->playFp==0) return;
	this->playFp(pitch, length);
}
void SimulatorControl::setSoundDuration(unsigned int len)
{
	if (this->setSoundDurationFp==0) return;
	this->setSoundDurationFp(len);
}
void SimulatorControl::setSoundInternote(unsigned int len)
{
	if (this->setSoundInternoteFp==0) return;
	this->setSoundInternote(len);
}
void SimulatorControl::stopSound()
{
	if (this->stopSoundFp==0) return;
	this->stopSoundFp();
}
void SimulatorControl::beepSound()
{
	if (this->beepSoundFp==0) return;
	this->beepSoundFp();
}

void SimulatorControl::notifyLoopCount(unsigned int count)
{
	if (this->notifyLoopCountFp==0) return;
	this->notifyLoopCountFp(count);

}

void SimulatorControl::setSensorName(int port, char *name)
{
	for (int i=0;i<nameLenMax;i++) {
		this->sensorName[port][i] = *(name+i);
		if (*(name+i)=='\0') break;
	}
	this->sensorName[port][nameLenMax-1]='\0';
}

char* SimulatorControl::getSensorName(int port)
{
	return this->sensorName[port];
}

void SimulatorControl::setMotorName(int port, char* name)
{
	port--;
	for (int i=0;i<nameLenMax;i++) {
		this->motorName[port][i] = *(name+i);
		if (*(name+i)=='\0') break;
	}
	this->motorName[port][nameLenMax-1]='\0';
}

char* SimulatorControl::getMotorName(int port)
{
	port--;
	return this->motorName[port];
}

void SimulatorControl::SetSensorValue(int port, int value)
{
	this->sensorValue[port] = value;
}

void SimulatorControl::SetBatteryLevel(int level)
{
	this->batteryLevel=level;
}

void SimulatorControl::SetKeyStatusToRobot(bool onoff, bool run, bool view, bool prog)
{
	int keys = 0;
	if (onoff) keys |= 0x01;
	if (run)   keys |= 0x02;
	if (view)  keys |= 0x04;
	if (prog)  keys |= 0x08;
	this->keyStatus=keys;
	if (this->keyTaskDelegator!=0) {
		this->keyTaskDelegator((void*)keys);
	}
}
void SimulatorControl::SendMessageToRobot(unsigned char msg)
{
	if (this->msgTaskDelegator!=0) {
		this->msgTaskDelegator((void*)msg);
	}
}

void SimulatorControl::SetDelegatorSetSensorModeFp(SetSensorModeFp fp)
{
	this->setSensorModeFp = fp;
}
void SimulatorControl::SetDelegatorSetLightBrightnessFp(SetLightBrightnessFp fp)
{
	this->setLightBrightnessFp=fp;
}
void SimulatorControl::SetDelegatorSetLightStatusFp(SetLightStatusFp fp)
{
	this->setLightStatusFp = fp;
}
void SimulatorControl::SetDelegatorSendMessageFp(SendMessageFp fp)
{
	this->sendMessageFp = fp;
}
void SimulatorControl::SetDelegatorReceiveMessageFp(ReceiveMessageFp fp)
{
	this->receiveMessageFp = fp;
}
void SimulatorControl::SetDelegatorClearMessageFp(ClearMessageFp fp)
{
	this->clearMessageFp = fp;
}
void SimulatorControl::SetDelegatorOrderMotorFp(OrderMotorFp fp)
{
	this->orderMotorFp = fp;
}
void SimulatorControl::SetDelegatorCputCLCDFp(CputCLCDFp fp)
{
	this->cputCLCDFp = fp;
}
void SimulatorControl::SetDelegatorCputWLCDFp(CputWLCDFp fp)
{
	this->cputWLCDFp = fp;
}
void SimulatorControl::SetDelegatorCputSLCDFp(CputSLCDFp fp)
{
	this->cputSLCDFp = fp;
}
void SimulatorControl::SetDelegatorClearLCDFp(ClearLCDFp fp)
{
	this->clearLCDFp = fp;
}
void SimulatorControl::SetDelegatorPlayFp(PlayFp fp)
{
	this->playFp = fp;
}
void SimulatorControl::SetDelegatorSetSoundDurationFp(SetSoundDurationFp fp)
{
	this->setSoundDurationFp = fp;
}
void SimulatorControl::SetDelegatorSetSoundInternoteFp(SetSoundInternoteFp fp)
{
	this->setSoundInternoteFp = fp;
}
void SimulatorControl::SetDelegatorStopSoundFp(StopSoundFp fp)
{
	this->stopSoundFp = fp;
}
void SimulatorControl::SetDelegatorBeepSoundFp(BeepSoundFp fp)
{
	this->beepSoundFp = fp;
}

void SimulatorControl::SetDelegatorNotifyLoopCount(NotifyLoopCountFp fp)
{
	this->notifyLoopCountFp = fp;
}
